
package com.light.gamecomponents.gravitylight;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.light.gamecomponents.physics.MovingBar;
import com.light.gamecomponents.physics.PhysicsComponents;
import com.light.gamecomponents.physics.RollerPhysicsWorld;
import com.light.mapcomponents.PanelComponents;
import com.light.roller.Player;
import com.light.roller.Roller;

public class PlayerGravityLight extends GravityLight implements IUpdateHandler{
	
	protected Player m_player;
	protected Body m_dummyBody;
	public PlayerGravityLight(Roller game, float x, float y, Player player) 
	{
		super(game, "PlayerGravityLight", x, y, 200, 200);
		m_player = player;
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		if (m_player != null && m_sprite != null)
		{
			m_sprite.setPosition(m_player.getX() - m_sprite.getBaseWidth() / 2 + m_player.getSizeX() / 2, 
					             m_player.getY() - m_sprite.getBaseHeight() / 2 + m_player.getSizeY() / 2);
		}
	}
	
	protected LoopEntityModifier m_nowModifiler;
	public void hide()
	{
		
		if(m_sprite.getAlpha()==0)return;
		LoopEntityModifier EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(1.0f,0.3f,0)));
		if (m_nowModifiler != null)m_sprite.unregisterEntityModifier(m_nowModifiler);
		m_nowModifiler = EntityModifier;
		m_sprite.registerEntityModifier(m_nowModifiler);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		m_sprite = new Sprite(m_initialPositionX,
				              m_initialPositionY,
				              m_textureRegion);
		m_sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_sprite.setAlpha(0.3f);
		
		
		((RollerPhysicsWorld)physicsWorld).registerUpdateHandler(this);
		scene.attachChild(m_sprite);
	}
	
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_sprite);
		((RollerPhysicsWorld)physicsWorld).unregisterUpdateHandler(this);
		m_sprite = null;
	}
	
	@Override
	public void onLoadEngine() 
	{
		
	}
	@Override
	public boolean collidesWith(PhysicsComponents PC) 
	{
		return PC.isInLight(this);
	}
	
	@Override
	public boolean collidesWith(PanelComponents PC) {
		return PC.isInLight(this);
	}



	@Override
	public void reset() {
		
	}



	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}


}
